//
//  ListController.m
//  pa
//
//  Created by HaiJiao Chen on 12-7-25.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ListController.h"

@implementation ListController
@synthesize delegate;

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:DataSourceChangeNotification object:data];
    [bg removeFromSuperview];
    tableView.delegate = nil;
    tableView.dataSource = nil;
    [tableView reloadData];
    [tableView release];
    [data release];
    [reuseIdentifer release];
    [super dealloc];
}

-(void)setupController:(UITableView*)_tableView data:(id<DataSource>)_data reuse:(NSString*)reuse
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:DataSourceChangeNotification object:data];
    tableView.delegate = nil;
    tableView.dataSource = nil;
    [tableView reloadData];
    [tableView release];
    [data release];
    [reuseIdentifer release];
    
    reuseIdentifer = [reuse retain];
    data = [_data retain];
    tableView = [_tableView retain];
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView reloadData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changed:) name:DataSourceChangeNotification object:data];
}

-(void)changed:(NSNotification*)notification
{
    if ([notification object] == data)
    {
        [tableView reloadData];
    }
}

- (void)setBGWithColor1:(UIColor*)color1 color2:(UIColor*)color2 height:(int)height bg:(ListControllerBG*)_bg
{
    UIView* v = [[UIView alloc] initWithFrame:tableView.frame];
    v.clipsToBounds = YES;
    bg = _bg;
    cellHeight = height;
    [tableView.superview insertSubview:v belowSubview:tableView];
    CGFloat tableViewHeight = tableView.frame.size.height;
    int tileCount = ceilf( tableViewHeight/(cellHeight*2)) + 1;
    bg.color1 = color1;
    bg.color2 = color2;
    bg.cellHeight = height;
    bg.frame = CGRectMake(0, 0, tableView.frame.size.width, cellHeight*tileCount*2);
    [bg setNeedsDisplay];
    [v addSubview:bg];
    [v release];
}


-(void)createSubView:(UITableViewCell*)cell
{
}
-(void)setupNewCell:(UITableViewCell*)cell obj:(id<NSObject>)obj indexPath:(NSIndexPath*)indexPath
{
}
- (CGFloat)heightForRowAtIndexPath:(NSIndexPath *)indexPath obj:(id<NSObject>)obj
{
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [data.data count];
}

- (UITableViewCell*)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id obj = [data.data objectAtIndex:[indexPath row]];
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifer];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifer] autorelease];
        [self createSubView:cell];
    }
    [self setupNewCell:cell obj:obj indexPath:indexPath];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self heightForRowAtIndexPath:indexPath obj:[data.data objectAtIndex:[indexPath row]]];
}

- (void)tableView:(UITableView *)_tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [delegate tableView:tableView didSelectRowAtIndexPath:indexPath];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (cellHeight > 0)
    {
        CGFloat offsetY = tableView.contentOffset.y;
        CGFloat tileOffset = offsetY - floorf(offsetY / (cellHeight*2)) * (cellHeight*2);
        CGRect frame = bg.frame;
        frame.origin.y = -tileOffset;
        bg.frame = frame;
    }
}
@end
